//
//  ViewController.swift
//  验证手机号
//
//  Created by cvicseks-mac1 on 16/3/22.
//  Copyright © 2016年 cvicseks-mac1. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let MOBILE = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"
    let CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"
    let CU = "^1(3[0-2]|5[256]|8[569]|7)\\d{8}$"
    let all = "^1[3|4|5|7|8][0-9]\\d{8}$"
    let EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w{2,4}$"
    var text:UITextField = UITextField(frame:CGRect(x: 60, y: 100, width: 200, height: 30))
    var button:UIButton = UIButton(frame: CGRect(x: 110, y: 200, width: 100, height: 30))
    override func viewDidLoad() {
        super.viewDidLoad()
        text.placeholder = "请输入手机号"
        text.textColor = UIColor.blackColor()
        text.borderStyle = UITextBorderStyle.RoundedRect
        self.view.addSubview(text)
        
        button.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("click", forState: UIControlState.Normal)
        button.backgroundColor = UIColor.blueColor()
        self.view.addSubview(button)
    }
    func click(){
        let regextestmobile:NSPredicate = NSPredicate(format: "SELF MATCHES %@", MOBILE)
        let regextestcm:NSPredicate = NSPredicate(format: "SELF MATCHES %@", CM)
        let regextestcu:NSPredicate = NSPredicate(format: "SELF MATCHES %@", CU)
        let regextestall:NSPredicate = NSPredicate(format: "SELF MATCHES %@", all)
        let regextestemail:NSPredicate = NSPredicate(format: "SELF MATCHES %@", EMAIL)
        if(regextestmobile.evaluateWithObject(text.text) == true){
            NSLog("移动")
        }else if(regextestcm.evaluateWithObject(text.text) == true){
            NSLog("联通")
        }else if(regextestcu.evaluateWithObject(text.text) == true){
            NSLog("电信")
        }else if(regextestall.evaluateWithObject(text.text) == true){
            NSLog("手机")
        }else if(regextestemail.evaluateWithObject(text.text) == true){
            NSLog("邮箱")
        }else{
            NSLog("error")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

